import os
import sys
import django

# 设置Django环境
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'pupaoshop.settings')
django.setup()

from api.models import Banner, Category, Goods
from django.contrib.auth.models import User


def create_super_user():
    """创建超级用户"""
    try:
        User.objects.get(username='admin')
        print('超级用户已存在')
    except User.DoesNotExist:
        User.objects.create_superuser('admin', 'admin@example.com', 'admin123')
        print('超级用户创建成功: 用户名admin, 密码admin123')


def create_banners():
    """创建轮播图数据"""
    banners = [
        {
            'image_url': 'https://via.placeholder.com/750x300/FF5733/FFFFFF?text=满99减30',
            'link': '/pages/promotion/promotion?id=1',
            'sort': 1
        },
        {
            'image_url': 'https://via.placeholder.com/750x300/33FF57/000000?text=今日生鲜特价',
            'link': '/pages/promotion/promotion?id=2',
            'sort': 2
        },
        {
            'image_url': 'https://via.placeholder.com/750x300/3357FF/FFFFFF?text=新人专享优惠',
            'link': '/pages/promotion/promotion?id=3',
            'sort': 3
        }
    ]

    for banner_data in banners:
        Banner.objects.create(**banner_data)
        print(f'创建轮播图: {banner_data["image_url"]}')


def create_categories():
    """创建分类数据"""
    # 一级分类
    categories = [
        {'name': '水果', 'icon_url': 'https://via.placeholder.com/50x50/FF5733/FFFFFF?text=水果'},
        {'name': '蔬菜', 'icon_url': 'https://via.placeholder.com/50x50/33FF57/000000?text=蔬菜'},
        {'name': '肉蛋', 'icon_url': 'https://via.placeholder.com/50x50/FFC300/000000?text=肉蛋'},
        {'name': '粮油', 'icon_url': 'https://via.placeholder.com/50x50/3357FF/FFFFFF?text=粮油'},
        {'name': '日用', 'icon_url': 'https://via.placeholder.com/50x50/C70039/FFFFFF?text=日用'},
        {'name': '零食', 'icon_url': 'https://via.placeholder.com/50x50/900C3F/FFFFFF?text=零食'},
        {'name': '酒水', 'icon_url': 'https://via.placeholder.com/50x50/581845/FFFFFF?text=酒水'},
        {'name': '母婴', 'icon_url': 'https://via.placeholder.com/50x50/1A5276/FFFFFF?text=母婴'}
    ]

    # 子分类
    sub_categories = {
        '水果': ['苹果', '香蕉', '草莓', '橙子', '葡萄'],
        '蔬菜': ['白菜', '萝卜', '西红柿', '黄瓜', '土豆'],
        '肉蛋': ['猪肉', '牛肉', '鸡肉', '鸡蛋', '鸭蛋'],
        '粮油': ['大米', '面粉', '食用油', '面条', '饺子'],
        '日用': ['纸巾', '洗发水', '沐浴露', '牙膏', '牙刷'],
        '零食': ['薯片', '巧克力', '饼干', '糖果', '坚果'],
        '酒水': ['啤酒', '白酒', '红酒', '饮料', '矿泉水'],
        '母婴': ['奶粉', '纸尿裤', '婴儿车', '玩具', '童装']
    }

    # 创建一级分类
    category_objects = {}
    for category_data in categories:
        category = Category.objects.create(**category_data)
        category_objects[category.name] = category
        print(f'创建分类: {category.name}')

    # 创建子分类
    for parent_name, subs in sub_categories.items():
        parent = category_objects[parent_name]
        for sub_name in subs:
            Category.objects.create(
                name=sub_name,
                icon_url=f'https://via.placeholder.com/50x50/CCCCCC/000000?text={sub_name}',
                parent=parent
            )
            print(f'创建子分类: {parent_name} -> {sub_name}')


def create_goods():
    """创建商品数据"""
    # 获取分类
    fruit = Category.objects.get(name='水果')
    vegetable = Category.objects.get(name='蔬菜')
    meat = Category.objects.get(name='肉蛋')

    # 商品数据
    goods = [
        # 水果
        {
            'name': '新鲜草莓',
            'image_url': 'https://via.placeholder.com/300x300/FF5733/FFFFFF?text=草莓',
            'original_price': 29.90,
            'current_price': 19.90,
            'sales': 1200,
            'is_hot': True,
            'category': fruit,
            'spec': '250g',
            'limit_buy': True
        },
        {
            'name': '进口车厘子',
            'image_url': 'https://via.placeholder.com/300x300/900C3F/FFFFFF?text=车厘子',
            'original_price': 89.90,
            'current_price': 59.90,
            'sales': 800,
            'is_hot': True,
            'category': fruit,
            'spec': '500g',
            'limit_buy': True
        },
        {
            'name': '红富士苹果',
            'image_url': 'https://via.placeholder.com/300x300/FFC300/000000?text=苹果',
            'original_price': 15.90,
            'current_price': 9.90,
            'sales': 1500,
            'is_hot': True,
            'category': fruit,
            'spec': '1kg',
            'limit_buy': False
        },
        # 蔬菜
        {
            'name': '有机白菜',
            'image_url': 'https://via.placeholder.com/300x300/33FF57/000000?text=白菜',
            'original_price': 5.90,
            'current_price': 2.90,
            'sales': 2000,
            'is_hot': True,
            'category': vegetable,
            'spec': '500g',
            'limit_buy': False
        },
        {
            'name': '新鲜西红柿',
            'image_url': 'https://via.placeholder.com/300x300/FF5733/FFFFFF?text=西红柿',
            'original_price': 6.90,
            'current_price': 3.90,
            'sales': 1800,
            'is_hot': True,
            'category': vegetable,
            'spec': '500g',
            'limit_buy': False
        },
        # 肉蛋
        {
            'name': '土鸡蛋',
            'image_url': 'https://via.placeholder.com/300x300/FFC300/000000?text=鸡蛋',
            'original_price': 19.90,
            'current_price': 12.90,
            'sales': 2500,
            'is_hot': True,
            'category': meat,
            'spec': '10个',
            'limit_buy': True
        }
    ]

    # 创建商品
    for goods_data in goods:
        Goods.objects.create(**goods_data)
        print(f'创建商品: {goods_data["name"]}')


def main():
    """主函数"""
    print('开始初始化数据...')

    # 创建超级用户
    create_super_user()

    # 创建轮播图
    create_banners()

    # 创建分类
    create_categories()

    # 创建商品
    create_goods()

    print('数据初始化完成！')


if __name__ == '__main__':
    main()